#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from logger_util import get_logger

logger = get_logger()


def check_env():
    if not os.path.exists("./tmp/packing.sh"):
        f = open("./tmp/packing.sh", "w")
        f.write("#!/bin/bash\n")
        f.write("\n")
        f.write("cd $1")
        f.write("\n")
        f.write("mkdir -p tmp")
        f.write("\n")
        f.write("zip -r $2 $3")
        f.close()


class Packing:

    @staticmethod
    def packing_all(directory):
        """
        打包指定目录下所有的文件夹,文件到一个压缩文件内．判断指定目录是否存在packing_all名称的压缩包．存在则不会进行二次压缩
        :param directory: 目录
        :return: 压缩文件路径
        """
        check_env()
        zip_name = "tmp/packing_all.zip"
        file_names = ""
        if os.path.isdir(directory):
            items = os.listdir(directory)
            for item in items:
                if item == 'tmp':
                    continue
                file_names += (" " + item)
        else:
            raise Exception(" directory not a dir ")
        if not os.path.exists(directory + "/" + zip_name):
            shell = "sh ./tmp/packing.sh {cd_dir} {zip_name} {file_names}".format(cd_dir=directory, zip_name=zip_name,
                                                                                  file_names=file_names)
            logger.info("run packing shell: {0}".format(shell))
            os.system(shell)
        return os.path.join(directory, zip_name)

    @staticmethod
    def packing_each(directory):
        """
        打包指定目录下所有的文件夹,文件,每一个文件或文件夹都有一个压缩包．判断指定目录是否存在相同名称的压缩包．存在则不会进行二次压缩
        :param directory: 目录
        :return path: 压缩文件路径list
        """
        check_env()
        path = []
        if os.path.isdir(directory):
            items = os.listdir(directory)
            for item in items:
                if item == 'tmp':
                    continue
                zip_name = "tmp/" + item + ".zip"
                if not os.path.exists(directory + "/" + zip_name):
                    shell = "sh ./tmp/packing.sh {cd_dir} {zip_name} {file_names}".format(cd_dir=directory,
                                                                                          zip_name=zip_name,
                                                                                          file_names=item)
                    logger.info("run packing shell: {0}".format(shell))
                    os.system(shell)
                path.append(os.path.join(directory, "tmp/" + item + ".zip"))
        return path

    @staticmethod
    def packing_path(path):
        """
        打包指定目录或文件,打包文件包含指定的目录或文件．判断指定目录是否存在相同名称的压缩包．存在则不会进行二次压缩
        :param path: str 目录或文件路径
        :return: 压缩文件路径
        """
        check_env()
        directory = path[0: path.rindex("/")]
        zip_name = "tmp/" + path[path.rindex("/") + 1:] + ".zip"
        if not os.path.exists(directory + "/" + zip_name):
            shell = "sh ./tmp/packing.sh {cd_dir} {zip_name} {file_names}".format(cd_dir=directory,
                                                                                  zip_name=zip_name,
                                                                                  file_names=path)
            logger.info("run packing shell: {0}".format(shell))
            os.system(shell)
        return os.path.join(directory, zip_name)

    @staticmethod
    def packing_all_exclude(directory, exclude):
        """
        打包指定目录所有文件夹,文件到一个压缩文件内,
        :param directory: str 指定目录
        :param exclude: list 排除指定目录
        :return:
        """
        # todo
        pass
